package com.apts.util;

import javax.servlet.http.HttpServletResponse;

/**
 * Response响应头信息设置<br>
 * 链式编程
 * 
 * @author lwh
 * @date 2016-06-03
 */
public class ResponseUtil {

	private HttpServletResponse response;

	/**
	 * 初始化ResponseUtil和response对象
	 * 
	 * @param response
	 * @return ResponseUtil
	 */
	public static ResponseUtil init(HttpServletResponse response) {
		ResponseUtil ru = new ResponseUtil();
		ru.response = response;
		return ru;
	}
	
	/**
	 * 获取设置后的Response
	 * @return
	 */
	public HttpServletResponse getResponse(){
		return this.response;
	}

	/**
	 * 跨域访问，Response响应头信息设置
	 * 
	 * @param domain
	 *            域名参数，有多个用“，”隔开
	 * @param methods
	 *            允许跨域请求方式：如GET、POST、DELETE、PUT，有多个用“，”隔开
	 * @return
	 */
	public ResponseUtil setAccessControlAllowOrigin(String domain, String methods) {
		this.response.setHeader("Access-Control-Allow-Origin", domain);
		this.response.setHeader("Access-Control-Request-Method", methods);
		this.response.setHeader("Access-Control-Allow-Credentials", "true");//告诉客户端可以在HTTP请求中带上Cookie
		this.response.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization,cache-control,Accept,X-Requested-With");
		return this;
	}

	/**
	 * 返回图片二进制流，Response响应头信息设置
	 */
	public ResponseUtil setStreamContentType(String contentType) {
		this.response.setHeader("Pragma", "No-cache");
		this.response.setHeader("Cache-Control", "no-cache");
		this.response.setDateHeader("Expires", 0);
		this.response.setContentType(contentType);
		return this;
	}

}
